package com.atguigu.demojuc.chap06;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

public class BlockingQueueDemo {
    public static void main(String[] args) {
       BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(3);
       new Thread(()->{
           for (int i = 1; i <= 10; i++) {
               try {
                   queue.put(i);
                   System.out.println("生产第" + i + "个馒头，" + "目前还剩" + queue.size() + "个馒头");
                   TimeUnit.SECONDS.sleep(1);
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }
           }
       },"生产者").start();
        new Thread(()->{
            for (int i = 1; i <= 10; i++) {
                try {
                    System.out.println("消费第" + queue.take() + "个馒头" + "目前还剩" + queue.size() + "个馒头");
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"消费者").start();
    }
}
